local skel = fk.CreateSkill {
  name = "lb_old__yunheng",
}

Fk:loadTranslationTable {
  ["lb_old__yunheng"] = "氲横",
  [":lb_old__yunheng"] = "出牌阶段限一次，你可以展示牌堆顶一张牌，若花色与此前展示牌均不同，你选择一项：<br>" ..
      "1.分配所有展示牌；<br>" ..
      "2.重复此流程；<br>" ..
      "3.弃置一名角色的一张牌，然后你执行其指定的<a href=':lb_old__yunheng'><font color='red'>〖氲横〗</font></a>的一项（本项每回合限一次）。" ..
      "你每回合首次发动〖氲横〗后，若未因此获得牌，本回合〖氲横〗可发动次数+1。",
  --"若你未获得展示牌，你将这些牌以任意顺序置于牌堆顶。",

  ["#lb_old__yunheng"] = "你可以发动〖氲横〗",
  ["#lb_old__yunheng_1"] = "分配所有展示牌",
  ["#lb_old__yunheng_2"] = "重复此流程",
  ["#lb_old__yunheng_3"] = "弃置一名角色的一张牌，然后你执行其指定的〖氲横〗的一项",

  ["#lb_old__yunheng-ask"] = "氲横：你可以令%dest分配所有展示牌，或取消后令其重复此流程",
  ["#lb_old__yunheng-dis"] = "氲横：弃置一名角色的一张牌",
  ["#lb_old__yunheng-give"] = "氲横：你可以将这些牌分配给其他角色，或取消后自己保留",

  ["$lb_old__yunheng1"] = "炉香袅孤碧，云缕霏数千。",
  ["$lb_old__yunheng2"] = "金鳞燃犀，洞若观火。",
}

skel:addEffect("active", {
  anim_type = "drawcard",
  prompt = "#lb_old__yunheng",
  can_use = function(self, player)
    return (player:usedSkillTimes("lb_old__yunheng", Player.HistoryPhase) - player:getMark("lb_old__yunheng_time-turn") < 1)
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local cards = {}
    local chong = false
    repeat
      table.insert(cards, room:getNCards(1)[1])
      room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, skel.name, nil, true, player)
      for _, suit in ipairs({ Card.Heart, Card.Spade, Card.Club, Card.Diamond }) do
        if #table.filter(cards, function(id) return Fk:getCardById(id).suit == suit end) > 1 then
          chong = true
          cards = {}
          break
        end
      end
      if chong then break end
      local all = { "#lb_old__yunheng_1", "#lb_old__yunheng_2", "#lb_old__yunheng_3" }
      local ask = table.clone(all)
      local players = table.filter(room:getAlivePlayers(), function(p)
        return not p:isNude()
      end)
      if #players == 0 or player:getMark("lb_old__yunheng-turn") ~= 0 then
        table.removeOne(ask, "#lb_old__yunheng_3")
      end
      local choice = room:askToChoice(player, {
        choices = ask,
        skill_name = skel.name,
        all_choices = all
      })
      if choice == "#lb_old__yunheng_1" then
        --room:obtainCard(player, cards, true, fk.ReasonJustMove, player, skel.name)
        break
      elseif choice == "#lb_old__yunheng_3" then
        room:setPlayerMark(player, "lb_old__yunheng-turn", 1)
        local t = room:askToChoosePlayers(player, {
          targets = players,
          min_num = 1,
          max_num = 1,
          skill_name = skel.name,
          prompt = "#lb_old__yunheng-dis",
          cancelable = false,
        })[1]
        local cid = room:askToChooseCard(player, {
          target = t,
          flag = "he",
          skill_name = skel.name,
          prompt = "#lb_old__yunheng-dis",
        })
        room:throwCard({ cid }, skel.name, t, player)
        if room:askToSkillInvoke(t, { skill_name = skel.name, prompt = "#lb_old__yunheng-ask::" .. player.id }) then
          --room:obtainCard(player, cards, true, fk.ReasonJustMove, player, skel.name)
          break
        end
      end
      player:broadcastSkillInvoke("lb_old__yunheng")
    until chong
    --[[
    if chong then
      cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
      local result = room:askToGuanxing(player, {
        cards = cards,
        top_limit = nil,
        bottom_limit = { 0, 0 },
        skill_name = skel.name,
        skip = true,
        area_names = { "Top", "toObtain" },
      })
      if #result.top > 0 then
        room:moveCardTo(table.reverse(result.top), Card.DrawPile, nil, fk.ReasonPut, skel.name, nil, true, player)
      end
    end
    --]]
    local addTime = true
    while not player.dead and #cards > 0 do
      local tos, ids = room:askToChooseCardsAndPlayers(player, {
        min_num = 1,
        max_num = 1,
        min_card_num = 1,
        max_card_num = #cards,
        targets = room:getOtherPlayers(player),
        pattern = tostring(Exppattern { id = cards }),
        skill_name = skel.name,
        prompt = "#lb_old__yunheng-give",
        cancelable = true,
        expand_pile = cards,
      })
      if #tos > 0 and #ids > 0 then
        for _, id in ipairs(ids) do
          table.removeOne(cards, id)
        end
        room:moveCardTo(ids, Card.PlayerHand, tos[1], fk.ReasonJustMove, skel.name, nil, false, player)
        --if #cards == 0 then break end
      else
        room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, skel.name, nil, false, player)
        addTime = false
        break
      end
    end
    room:cleanProcessingArea()
    if player:usedSkillTimes(skel.name) == 1 and addTime then
      room:setPlayerMark(player, "lb_old__yunheng_time-turn", 1)
    end
  end,
})

return skel